package gw.com.android.ui.onlive;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import gw.com.android.app.AppContances;
import gw.com.android.app.GTConfig;
import gw.com.android.utils.ColorThemeUtil;

/**
 * 一键晒单的模板
 *
 * @author jett
 * @since 2018-03-07.
 */
abstract class DiscussTemplet {

    protected final Context context;

    private DiscussTemplet(Context context) {
        this.context = context;
    }

    abstract View createView();

    static List<DiscussTemplet> buildTemplets(Context context, final TradeResultData data) {
        List<DiscussTemplet> list = new ArrayList<>();
        list.add(new DiscussTemplet(context) {
            @Override
            View createView() {
                return createTemplet1(this.context, data);
            }
        });
        list.add(new DiscussTemplet(context) {
            @Override
            View createView() {
                return createTemplet2(this.context, data);
            }
        });
        list.add(new DiscussTemplet(context) {
            @Override
            View createView() {
                return createTemplet3(this.context, data);
            }
        });
        return list;
    }

    /**
     * 模板一  的 View
     */
    private static View createTemplet1(Context context, TradeResultData data) {
        View view = View.inflate(context, R.layout.view_discuss_templet1, null);
        View rootView = view.findViewById(R.id.discuss_templet_layout);
        TextView tvSymbol = (TextView) rootView.findViewById(R.id.discuss_templet_symbol);
        TextView tvAmount = (TextView) rootView.findViewById(R.id.discuss_templet_amount);
        TextView tvProduct = (TextView) rootView.findViewById(R.id.discuss_templet_product);
        TextView tvBuyPrice = (TextView) rootView.findViewById(R.id.discuss_templet_buy_price);
        TextView tvSellPrice = (TextView) rootView.findViewById(R.id.discuss_templet_sell_price);
        TextView tvBuyTime = (TextView) rootView.findViewById(R.id.discuss_templet_buy_time);
        TextView tvSellTime = (TextView) rootView.findViewById(R.id.discuss_templet_sell_time);
        Typeface font = Typeface.createFromAsset(context.getResources().getAssets(), "fonts/DIN_Medium.ttf");
        tvSymbol.setTypeface(font);
        tvAmount.setTypeface(font);
        tvBuyPrice.setTypeface(font);
        tvSellPrice.setTypeface(font);
        if (data != null) {
            if (GTConfig.instance().getPrefColorConfig().equals(GTConfig.COLOR_GREEN_RISE)) {
                if (data.profit) {
                    rootView.setBackgroundResource(R.mipmap.a_show_green_template1);
                    tvProduct.setTextColor(ColorThemeUtil.instance().RiseColor);
                } else {
                    rootView.setBackgroundResource(R.mipmap.a_show_red_template1);
                    tvProduct.setTextColor(ColorThemeUtil.instance().FallColor);
                }
            } else {
                if (data.profit) {
                    rootView.setBackgroundResource(R.mipmap.a_show_red_template1);
                    tvProduct.setTextColor(ColorThemeUtil.instance().RiseColor);
                } else {
                    rootView.setBackgroundResource(R.mipmap.a_show_green_template1);
                    tvProduct.setTextColor(ColorThemeUtil.instance().FallColor);
                }
            }
            tvSymbol.setText(data.profit ? "$" : "-$");
            tvAmount.setText(data.amount);
            if (data.tradeDir == AppContances.TRADE_TYPE_BUY) {
                tvProduct.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_product_colse), data.product, data.quantity));
            } else {
                tvProduct.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_product_open), data.product, data.quantity));
            }
            tvBuyPrice.setText("$" + data.openPrice);
            tvSellPrice.setText("$" + data.closePrice);
            tvBuyTime.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_buy_time), data.openTime));
            tvSellTime.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_sell_time), data.closeTime));
        }
        return view;
    }

    /**
     * 模板二  的 View
     */
    private static View createTemplet2(Context context, TradeResultData data) {
        View view = View.inflate(context, R.layout.view_discuss_templet2, null);
        TextView tvSymbol = (TextView) view.findViewById(R.id.discuss_templet_symbol);
        TextView tvAmount = (TextView) view.findViewById(R.id.discuss_templet_amount);
        TextView tvProduct = (TextView) view.findViewById(R.id.discuss_templet_product);
        TextView tvBuyPrice = (TextView) view.findViewById(R.id.discuss_templet_buy_price);
        TextView tvSellPrice = (TextView) view.findViewById(R.id.discuss_templet_sell_price);
        TextView tvBuyTime = (TextView) view.findViewById(R.id.discuss_templet_buy_time);
        TextView tvSellTime = (TextView) view.findViewById(R.id.discuss_templet_sell_time);
        Typeface font = Typeface.createFromAsset(context.getResources().getAssets(), "fonts/DIN_Medium.ttf");
        tvSymbol.setTypeface(font);
        tvAmount.setTypeface(font);
        tvBuyPrice.setTypeface(font);
        tvSellPrice.setTypeface(font);
        if (data != null) {
            tvSymbol.setText(data.profit ? "$" : "-$");
            tvAmount.setText(data.amount);
            if (data.tradeDir == AppContances.TRADE_TYPE_BUY) {
                tvProduct.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_product_colse), data.product, data.quantity));
            } else {
                tvProduct.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_product_open), data.product, data.quantity));
            }
            tvBuyPrice.setText("$" + data.openPrice);
            tvSellPrice.setText("$" + data.closePrice);
            tvBuyTime.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_buy_time), data.openTime));
            tvSellTime.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_sell_time), data.closeTime));
        }
        return view;
    }

    /**
     * 模板三  的 View
     */
    private static View createTemplet3(Context context, TradeResultData data) {
        View view = View.inflate(context, R.layout.view_discuss_templet3, null);
        TextView tvSymbol = (TextView) view.findViewById(R.id.discuss_templet_symbol);
        TextView tvAmount = (TextView) view.findViewById(R.id.discuss_templet_amount);
        TextView tvProduct = (TextView) view.findViewById(R.id.discuss_templet_product);
        TextView tvBuyPrice = (TextView) view.findViewById(R.id.discuss_templet_buy_price);
        TextView tvSellPrice = (TextView) view.findViewById(R.id.discuss_templet_sell_price);
        TextView tvBuyTime = (TextView) view.findViewById(R.id.discuss_templet_buy_time);
        TextView tvSellTime = (TextView) view.findViewById(R.id.discuss_templet_sell_time);
        Typeface font = Typeface.createFromAsset(context.getResources().getAssets(), "fonts/DIN_Medium.ttf");
        tvSymbol.setTypeface(font);
        tvAmount.setTypeface(font);
        tvBuyPrice.setTypeface(font);
        tvSellPrice.setTypeface(font);
        if (data != null) {
            tvSymbol.setText(data.profit ? "$" : "-$");
            tvAmount.setText(data.amount);
            if (data.tradeDir == AppContances.TRADE_TYPE_BUY) {
                tvProduct.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_product_colse), data.product, data.quantity));
            } else {
                tvProduct.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_product_open), data.product, data.quantity));
            }
            tvBuyPrice.setText("$" + data.openPrice);
            tvSellPrice.setText("$" + data.closePrice);
            tvBuyTime.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_buy_time), data.openTime));
            tvSellTime.setText(String.format(Locale.getDefault(), context.getString(R.string.discuss_auto_sell_time), data.closeTime));
        }
        return view;
    }

}
